﻿using System;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using plust.Clases;
using plust.Persistencia;
using plust.Content;
using FirstFloor.ModernUI.Windows.Controls;
using System.Diagnostics;

namespace plust.Content
{
    /// <summary>
    /// Interaction logic for ModificarStockDialog.xaml
    /// </summary>
    public partial class ModificarStockDialog : UserControl
    {
        private Producto producto;
        private List<int> idsCategoria = new List<int>();
        private PersistenciaDB persistencia = new PersistenciaDB();

        

        public ModificarStockDialog()
        {
            InitializeComponent();
            DataTable data = persistencia.obtenerCategorias();
            if (data != null)
            {
                for (int i = 0; i < data.Rows.Count; i++)
                {
                    this.cbCategoria.Items.Add(data.Rows[i]["nombre_categoria"]);
                    this.idsCategoria.Insert(i, int.Parse(data.Rows[i]["id_categoria"].ToString()));
                }
            }
        }

        public ModificarStockDialog(Producto producto): this()
        {
            DataTable data = persistencia.obtenerCategorias();

            this.producto = producto;
            tbCodigoBarras.Text = producto.sku;
            tbPrecioNeto.Text = producto.precio_neto.ToString();
            tbPrecioVenta.Text = producto.precio_venta.ToString();
            tbStockCritico.Text = producto.stock_critico.ToString();
            tbStockReal.Text = producto.stock_real.ToString();
            tbDescripcion.Text = producto.descripcion;

            if (data != null)
            {
                for (int i = 0; i < data.Rows.Count; i++)
                {
                    
                    if (int.Parse(data.Rows[i]["id_categoria"].ToString()) == producto.id_categoria)
                    {
                        this.cbCategoria.Items.Add(data.Rows[i]["nombre_categoria"]);
                        this.cbCategoria.SelectedValue = data.Rows[i]["nombre_categoria"].ToString();
                        break;
                    }
                }
            }
            
        }

        private void btActualizar_Click(object sender, RoutedEventArgs e)
        {
            persistencia = new PersistenciaDB();

            this.producto.stock_real = Convert.ToInt32(this.tbStockReal.Text);

            Debug.WriteLine(this.producto.stock_real);

            if (persistencia.actualizarProducto(this.producto))
            {
                new ModernDialog
                {
                    Title = "CORRECTO",
                    Content = "Producto actualizado.",

                }.ShowDialog();
            }
            else
            {
                new ModernDialog
                {
                    Title = "ERROR",
                    Content = "Se ha producido un error inesperado."
                }.ShowDialog();
            }

            Window.GetWindow(this).Close();
        }

        private void btCancelar_Click(object sender, RoutedEventArgs e)
        {
            Window.GetWindow(this).Close();
        }

        private void btAumentarStock_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                int cant = Convert.ToInt32(this.tbStockReal.Text);

                cant++;

                this.tbStockReal.Text = cant.ToString();

            } catch(System.FormatException ex)
            {
                this.tbStockReal.Text = string.Empty;
            }
            
        }

        private void btDisminuirStock_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                int cant = Convert.ToInt32(this.tbStockReal.Text);

                cant--;

                if( cant >= 0 ){

                    this.tbStockReal.Text = cant.ToString();
                }
                
            }
            catch (System.FormatException ex)
            {
                this.tbStockReal.Text = string.Empty;
            }
        }

    }
}
